{% extends '__layout_3col.html' %}
{% block pageTitle %}{% parent %} - Оргуструктура компании{%endblock%}
{% block mainLayoutJsInclude %}
    {% parent%}
    <script type="text/javascript" src="/js/organization.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            {% for org in organization %}
                {% if org.data.parent_id %}
                    arrangeSubdivisions('{{org.data.organization_id}}', '{{org.data.parent_id}}');
                {% endif %}
            {% endfor %}
            {% for user in companyUsers %}
                {% if user.data.organization_id %}
                    placeUserAtSubdivisionTable('{{user.data.user_id}}','{{user.data.organization_id}}');
                {% endif %}
            {% endfor %}
        });
    </script>
{% endblock %}

{% block layoutLeftColumn %}
    {% include '_left_menu_staff.html' %}
{% endblock %}


{% block layoutCenterColumn %}
    <div class="block">
        <h2>Оргуструктура компании</h2>
        <div class="block" style="background: #f0f0f0;">
            <a href="javascript:void(0);" onclick="$('#addOrganizationBlock').toggle('fast');">Добавить подразделение</a>
            <div id="addOrganizationBlock" style="display: none; font-size: 80%;">
                <form action="" method="post">
                    <input type="hidden" name="action" value="new_organization">
                    <table class="formTable" cellspacing="0">
                        <tr>
                            <td>
                                <strong>Название:</strong><br/>
                                <input name="edit_organization_name" value=""/>
                            </td>
                            <td>
                                <strong>Описание:</strong><br/>
                                <input name="edit_organization_description" value=""/>
                            </td>
                            <td>
                                <strong>Вышестоящее подразделение:</strong><br/>
                                <select name="edit_organization_parent_id">
                                    <option value="">----------</option>
                                    {% for orgg in organization %}
                                        {% if orgg.data.organization_id != org.data.organization_id and not orgg.data.organization_id|in(org.data.ext_children_ids) %}
                                            <option value="{{orgg.data.organization_id}}">{{orgg.data.name}}</option>
                                        {% endif %}
                                    {% endfor %}
                                </select>
                            </td>
                        </tr>
                    </table>
                    <input type="submit" value="Создать"/>
                </form>
            </div>
        </div>

        {% for org in organization %}
        <table class="organizationSubdivision" id="{{org.data.organization_id}}_table" title="{{org.data.name}}">
            <tr>
                <td>
                    <div class="container">
                        <div class="head">
                            {% if userCanModify %}
                                    <a href="javascript:void(0);" class="floatButtonLink" onClick="setModifyOrganization('{{org.data.organization_id}}');">ред.</a>
                            {% endif %}
                            {{org.data.name}}
                        </div>
                        {% if org.data.description %}
                            <div class="description">
                                {{org.data.description}}
                            </div>
                        {% endif %}
                            <div class="headUser">
                                <span>Руководителя нет</span>
                            </div>
                            <div class="usersList">
                            </div>
                        {% if userCanModify %}
                            <div class="usersList">
                                <span><a href="javascript:void(0);" class="dynamicLink" onClick="addUserToSubdivision('{{org.data.organization_id}}');">Добавить сотрудника</a></span>
                            </div>
                        {% endif %}
                    </div>
                    {% if userCanModify %}
                    <form name="editOrganizationForm" action="" method="post" style="display:none">
                        <div class="container">
                            <input type="hidden" name="action" value="modify_organization">
                            <input type="hidden" name="edit_organization_id" value="{{org.data.organization_id}}">
                            <div class="head">
                                Название подразделения:<br/>
                                <input name="edit_organization_name" value="{{org.data.name}}">
                            </div>
                            <div>
                                Вышестоящее подразделение:<br/>
                                <select name="edit_organization_parent_id">
                                    <option value="">----------</option>
                                    {% for orgg in organization %}
                                        {% if orgg.data.organization_id != org.data.organization_id and not orgg.data.organization_id|in(org.data.ext_children_ids) %}
                                            <option value="{{orgg.data.organization_id}}">{{orgg.data.name}}</option>
                                        {% endif %}
                                    {% endfor %}
                                </select>

                            </div>
                            <div class="description">
                                Описание:<br/>
                                <input name="edit_organization_description" value="{{org.data.description}}">
                                
                            </div>
                            <div class="headUser">
                                <span>Руководитель:</span>
                                <select name="edit_organization_head_user_id">
                                    <option value="">---------</option>
                                    {% for user in companyUsers %}
                                        <option value="{{ user.data.user_id}}">{{ macros.getUserName(user) }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                            <div>
                                <input type="submit" value="OK"/>
                                <input type="button" value="Отмена" onClick="setModifyOrganization('{{org.data.organization_id}}');"/>
                                <input type="button" value="Удалить" />
                            </div>
                        </div>
                        </form>
                    {% endif %}
                </td>
                <td class="children" style="display:none;">
                </td>
            </tr>
        </table>
        {% endfor %}

        {% if userCanModify %}
        <div id="assignUserBlock" style="display:none">
            <form action="" method="post">
                <input type="hidden" name="action" value="set_user_organization">
                <input type="hidden" name="add_user_organization_id" value="">
                <select name="add_user_id">
                    <option value="">---------</option>
                    {% for user in companyUsers %}
                        <option value="{{ user.data.user_id}}">{{ macros.getUserName(user) }}</option>
                    {% endfor %}
                </select>
                <input type="submit" value="OK"/>
            </form>
        </div>
        {% endif %}

    </div>
{% endblock %}

{% block layoutRightColumn %}
    <div class="block">
        <h2>Нераспределённые сотрудники</h2>
        <div id="freeUsers">
            {% for user in companyUsers %}
                <div class="organizationUser" id="{{ user.data.user_id}}_user">
                    {{ macros.userName(user) }}
                    {% if user.data.organization_id and userCanModify %}
                        <a href="javascript:void(0);" class="closeButton" onClick="removeUserOrganization('{{ user.data.user_id}}')">x</a>
                    {% endif %}
                    <br/>
                    <span class="userPosition">{{ user.data.position_name|default('нет должности') }}</span>

                </div>
            {% endfor %}

        </div>
    </div>
{% endblock %}

